<?php

namespace addons\Calendar\common\models;

use common\models\merchant\Member;
use Yii;

/**
 * This is the model class for table "{{%addon_oa_calendar}}".
 *
 * @property int $id ID
 * @property int $member_id 用户
 * @property string $begin_date 开始日期
 * @property string $end_date 结束日期
 * @property int $remind 提醒时间
 * @property int $type 日程类型
 * @property string $attend 与会人员
 * @property int $address 地点
 * @property int $content 描述
 * @property int $repetition 1重复0不重复
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class Calendar extends \common\models\base\BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_calendar}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['begin_date', 'end_date', 'remind', 'cate_id', 'address', 'content'], 'required'],
            [['merchant_id','member_id', 'remind', 'cate_id', 'repetition', 'status', 'created_at', 'updated_at'], 'integer'],
            [['begin_date', 'end_date','attend'], 'safe'],
            [['content','address'], 'string', 'max' => 64],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => '所有者',
            'begin_date' => '开始日期',
            'end_date' => '结束日期',
            'remind' => '提醒时间',
            'cate_id' => '日程类型',
            'attend' => '与会人员',
            'address' => '地点',
            'content' => '描述',
            'repetition' => '重复',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    /**
     * 关联用户
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMember()
    {
        return $this->hasOne(\common\models\merchant\Member::class, ['id' => 'member_id']);
    }

    public function beforeSave($insert)
    {
        if ($insert) {
            if(Yii::$app->id == 'backend'||Yii::$app->id == 'merchant')
                $member_id = Yii::$app->user->id;
            else
                $member_id = Yii::$app->user->identity->member_id;
            $this->member_id = $member_id;
            $this->merchant_id = Yii::$app->user->identity->merchant_id;
            //发送消息
            if (is_array($this->attend)) {
                foreach ($this->attend as $id) {
                    $content = '[日程]' . $this->content . '(' . $this->begin_date . '至' . $this->end_date . ')';
                    $member = Member::find()->andWhere(['id'=>$id])->one();
                    if($this->remind==0) Yii::$app->services->sms->send($member->mobile, '未读日程', 'message');
                    if($this->remind==1) Yii::$app->services->backendNotify->createMessage($content, $member_id, $id);
                }
            }
        }
        if (is_array($this->attend)) $this->attend = implode(',', $this->attend);

        return parent::beforeSave($insert);
    }
    public function afterFind()
    {
        $this->attend = explode(',', $this->attend);
        parent::afterFind(); // TODO: Change the autogenerated stub
    }

    public function afterSave($insert, $changedAttributes)
    {


        parent::afterSave($insert, $changedAttributes);
    }
}
